package com.logistics.dispatch;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Dispatchdatabase;

public class Orderconsultservlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public Orderconsultservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.getRequestDispatcher("../dispatch/dispatch_search_order.jsp").forward(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		if(request.getParameter("operation").equals("query"))
		{
			Dispatchdatabase ddb=new Dispatchdatabase();
			String fdate=request.getParameter("d4311");
			String ldate=request.getParameter("d4312");
			String type=request.getParameter("order_type");
			String p_name=request.getParameter("p_name");
			String u_name=request.getParameter("u_name");
			ArrayList<Mythicorder> orders=null;
			orders=ddb.consult_orders(fdate,ldate,type);
			for(Mythicorder temp:orders)
			{
				temp.setProduct_name(ddb.search_product_name(temp.getProduct_id()));
			}
			request.setAttribute("u_n", u_name);
			request.setAttribute("p_n", p_name);
			request.setAttribute("array", orders);
			request.getRequestDispatcher("../dispatch/dispatch_search_order.jsp").forward(request,response);
		}
		else if(request.getParameter("operation").equals("detail"))
		{
			int order_id=Integer.valueOf(request.getParameter("order_id"));
			Dispatchdatabase ddb=new Dispatchdatabase();
			Mythicorder mo=new Mythicorder();
			mo=ddb.consult_order(order_id);
			mo.setProduct_name(ddb.search_product_name(mo.getProduct_id()));
			request.setAttribute("ord", mo);
			ArrayList<Integer> stations=ddb.consult_station_ids();
			request.setAttribute("substations", stations);
			request.getRequestDispatcher("../dispatch/dispatch_detail.jsp").forward(request,response);
		}
		else if(request.getParameter("operation").equals("schedule"))
		{
			int order_id=Integer.valueOf(request.getParameter("order_id"));
			int substation_id= Integer.valueOf(request.getParameter("substation_id"));
			System.out.println(substation_id);
			Dispatchdatabase ddb=new Dispatchdatabase();
			ddb.update_substation(order_id,substation_id);
			try {
				Schedule.schedule(substation_id, order_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.getRequestDispatcher("../dispatch/dispatch_search_order.jsp").forward(request,response);
			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
